<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <input type="text" id="ipt"/>

    <script>
        const Ipt = document.getElementById("ipt");

        function code(){
            console.log(1)
        }

        Ipt.oninput = thorttle(code,1000)
        //防抖
        // function debounce(fn,time){
        //     let timer = null;
        //     return function (e){
        //         clearTimeout(timer)
        //         timer = setTimeout(()=>{
        //             fn.call(this,e)
        //         },time)
        //     }
        // }

        //节流
        function thorttle(fn,time){
            let lastTime = 0;
            return function (e){
               const newTime = Date.now()
                if(newTime-lastTime < time){
                    return
                }
                lastTime = newTime

                fn.call(this,e)
            }
        }
    </script>
</body>
</html>